package com.uenpay.im.ui.old.activity.movement;

import com.uenpay.im.ui.old.activity.dynamic.PraiseSerializer;
import com.uenpay.im.ui.old.activity.dynamic.ReplySerializer;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetMovementDetailInvokeItem extends BaseHttpInvokeItem {
	public class GetMovementDetailInvokeItemResult extends HttpInvokeResult {
		public MovementEntity movementEntity = new MovementEntity();

	}

	public GetMovementDetailInvokeItemResult getOutput() {
		return (GetMovementDetailInvokeItemResult) GetResultObject();
	}

	public GetMovementDetailInvokeItem(int activityid) {
		String url = APIConfiguration.getBaseUrl()
				+ "Activity/Activity/getActivityDetail?activityid=" + activityid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetMovementDetailInvokeItemResult result = new GetMovementDetailInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}

		JSONObject data = json.optJSONObject("data");
		result.movementEntity = desMovementEntity(data);
		return result;
	}

	private MovementEntity desMovementEntity(JSONObject json) {
		MovementEntity movementEntity = new MovementEntity();
		movementEntity.movementAddress = json.optString("address");
		movementEntity.movementTitle = json.optString("title");
		movementEntity.movementPublishCustId = json.optString("custid");
		movementEntity.movementPublishNickName = json.optString("nickname");
		movementEntity.movementPublishDans = json.optString("dans");
		movementEntity.movementPublishArea = json.optString("area");
		movementEntity.movementStartTime = json.optString("starttime");
		movementEntity.movementFee = json.optString("fee");
		movementEntity.movementDescription = json.optString("description");
		movementEntity.movementCreateTime = json.optString("createtime");
		movementEntity.movementPersonNum = json.optInt("personnum");
		movementEntity.movementMemberCount = json.optInt("memberCount");
		movementEntity._isfavorited = json.optInt("_isfavorited");
		movementEntity.movementId = json.optInt("activityid");
		if (json.has("_praise")) {
			movementEntity._praise = json.optInt("_praise");
		}
		if (json.has("status")) {
			movementEntity.status = json.optInt("status");
		}
		if (json.has("_isjoin")) {
			movementEntity.isJoin = json.optInt("_isjoin");
		}
		JSONObject coverJson = json.optJSONObject("cover");
		if (coverJson != null && coverJson.has("fileid")
				&& coverJson.has("filehash")) {
			movementEntity.movementCoverFileHash = coverJson
					.optString("filehash");
			movementEntity.movementCoverFileId = coverJson.optString("fileid");
		}

		JSONArray likeArray = json.optJSONArray("praiselist");
		if (likeArray != null) {
			movementEntity.likelist = PraiseSerializer
					.deserializeUsers(likeArray);
		}
		JSONArray replyArray = json.optJSONArray("replylist");
		if (replyArray != null) {
			movementEntity.replylist = ReplySerializer
					.deserializeUsers(replyArray);
		}

		return movementEntity;
	}

}
